<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blogs/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blogs/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blogs/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blogs/images/apple-touch-icon.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/blogs/images/favicon-32x32.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/blogs/images/favicon-16x16.png?v=5.1.3">


  <link rel="mask-icon" href="/blogs/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="Node.js," />










<meta name="description" content="Node.js实现的后端注册和登录接口（更新后-附前端测试代码）一.以前接口实现1.实现注册接口 由于以前初学使用node.js的时候，写个后端接口测试能跑通，就没有关心性能和安全性的问题  以下是我以前的注册接口的实现（不安全版本） 步骤主要是三大步  引入后端需要的相关模块，这里测试接口，因此只是引入了express,和mysql 连接数据库，使用mysql 写接口  123456789101">
<meta property="og:type" content="article">
<meta property="og:title" content="node.js实现后端登录注册接口">
<meta property="og:url" content="https://louchaoyy.gitee.io/blogs/2021/11/04/node-js%E5%AE%9E%E7%8E%B0%E5%90%8E%E7%AB%AF%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C%E6%8E%A5%E5%8F%A3/index.html">
<meta property="og:site_name" content="Chao&#39;s blog">
<meta property="og:description" content="Node.js实现的后端注册和登录接口（更新后-附前端测试代码）一.以前接口实现1.实现注册接口 由于以前初学使用node.js的时候，写个后端接口测试能跑通，就没有关心性能和安全性的问题  以下是我以前的注册接口的实现（不安全版本） 步骤主要是三大步  引入后端需要的相关模块，这里测试接口，因此只是引入了express,和mysql 连接数据库，使用mysql 写接口  123456789101">
<meta property="og:locale">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2021/11/04/node-js%E5%AE%9E%E7%8E%B0%E5%90%8E%E7%AB%AF%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C%E6%8E%A5%E5%8F%A3/data_stream.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2021/11/04/node-js%E5%AE%9E%E7%8E%B0%E5%90%8E%E7%AB%AF%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C%E6%8E%A5%E5%8F%A3/data_stream_s.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2021/11/04/node-js%E5%AE%9E%E7%8E%B0%E5%90%8E%E7%AB%AF%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C%E6%8E%A5%E5%8F%A3/back.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2021/11/04/node-js%E5%AE%9E%E7%8E%B0%E5%90%8E%E7%AB%AF%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C%E6%8E%A5%E5%8F%A3/database.png">
<meta property="article:published_time" content="2021-11-04T07:01:18.000Z">
<meta property="article:modified_time" content="2022-03-10T13:01:51.603Z">
<meta property="article:author" content="Chaoyxy">
<meta property="article:tag" content="Node.js">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://louchaoyy.gitee.io/blogs/2021/11/04/node-js%E5%AE%9E%E7%8E%B0%E5%90%8E%E7%AB%AF%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C%E6%8E%A5%E5%8F%A3/data_stream.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://louchaoyy.gitee.io/blogs/2021/11/04/node-js实现后端登录注册接口/"/>





  <title>node.js实现后端登录注册接口 | Chao's blog</title>
  








<meta name="generator" content="Hexo 5.4.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blogs/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Chao's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blogs/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/blogs/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blogs/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://louchaoyy.gitee.io/blogs/blogs/2021/11/04/node-js%E5%AE%9E%E7%8E%B0%E5%90%8E%E7%AB%AF%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C%E6%8E%A5%E5%8F%A3/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blogs/images/cat.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Chao's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">node.js实现后端登录注册接口</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-11-04T15:01:18+08:00">
                2021-11-04
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blogs/2021/11/04/node-js%E5%AE%9E%E7%8E%B0%E5%90%8E%E7%AB%AF%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C%E6%8E%A5%E5%8F%A3/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2021/11/04/node-js实现后端登录注册接口/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="Node-js实现的后端注册和登录接口（更新后-附前端测试代码）"><a href="#Node-js实现的后端注册和登录接口（更新后-附前端测试代码）" class="headerlink" title="Node.js实现的后端注册和登录接口（更新后-附前端测试代码）"></a>Node.js实现的后端注册和登录接口（更新后-附前端测试代码）</h2><h2 id="一-以前接口实现"><a href="#一-以前接口实现" class="headerlink" title="一.以前接口实现"></a>一.以前接口实现</h2><h3 id="1-实现注册接口"><a href="#1-实现注册接口" class="headerlink" title="1.实现注册接口"></a>1.实现注册接口</h3><ul>
<li><p>由于以前初学使用<code>node.js</code>的时候，写个后端接口测试能跑通，就没有关心性能和安全性的问题</p>
</li>
<li><p>以下是我以前的注册接口的实现（不安全版本）</p>
<p>步骤主要是三大步</p>
<ul>
<li>引入后端需要的相关模块，这里测试接口，因此只是引入了<code>express</code>,和<code>mysql</code></li>
<li>连接数据库，使用<code>mysql</code></li>
<li>写接口</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//引入express模块</span></span><br><span class="line"><span class="keyword">const</span> &#123; application, request, response &#125; = <span class="built_in">require</span>(<span class="string">&#x27;express&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">&#x27;express&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> app = express()</span><br><span class="line"><span class="comment">//引入mysql</span></span><br><span class="line"><span class="keyword">const</span> mysql = <span class="built_in">require</span>(<span class="string">&#x27;mysql&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">//解决跨域问题</span></span><br><span class="line">app.all(<span class="string">&#x27;/*&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params">req, res, next</span>) </span>&#123;</span><br><span class="line">    res.header(<span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span>, <span class="string">&#x27;*&#x27;</span>)</span><br><span class="line">    res.header(<span class="string">&#x27;Access-Control-Allow-Headers&#x27;</span>, <span class="string">&#x27;X-Requested-With, mytoken&#x27;</span>)</span><br><span class="line">    res.header(<span class="string">&#x27;Access-Control-Allow-Headers&#x27;</span>, <span class="string">&#x27;X-Requested-With, Authorization&#x27;</span>)</span><br><span class="line">    res.setHeader(<span class="string">&#x27;Content-Type&#x27;</span>, <span class="string">&#x27;application/json;charset=utf-8&#x27;</span>)</span><br><span class="line">    res.header(<span class="string">&#x27;Access-Control-Allow-Headers&#x27;</span>, <span class="string">&#x27;Content-Type,Content-Length, Authorization, Accept,X-					Requested-With&#x27;</span>)</span><br><span class="line">    res.header(<span class="string">&#x27;Access-Control-Allow-Methods&#x27;</span>, <span class="string">&#x27;PUT,POST,GET,DELETE,OPTIONS&#x27;</span>)</span><br><span class="line">    res.header(<span class="string">&#x27;X-Powered-By&#x27;</span>, <span class="string">&#x27; 3.2.1&#x27;</span>)</span><br><span class="line">    <span class="keyword">if</span> (req.method == <span class="string">&#x27;OPTIONS&#x27;</span>) res.send(<span class="number">200</span>)</span><br><span class="line">    <span class="comment">/*让options请求快速返回*/</span> <span class="keyword">else</span> next()</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 创建连接</span></span><br><span class="line"><span class="keyword">const</span> db = mysql.createConnection(&#123;</span><br><span class="line">    <span class="attr">host</span>:<span class="string">&quot;127.0.0.1&quot;</span>,</span><br><span class="line">    <span class="attr">user</span>:<span class="string">&quot;root&quot;</span>,</span><br><span class="line">    <span class="attr">password</span>:<span class="string">&quot;y********9&quot;</span>,</span><br><span class="line">    <span class="attr">port</span>:<span class="number">3306</span>,</span><br><span class="line">    <span class="attr">database</span>:<span class="string">&quot;nodemysql&quot;</span>,</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">	写接口</span></span><br><span class="line"><span class="comment">	@params</span></span><br><span class="line"><span class="comment">    username string</span></span><br><span class="line"><span class="comment">    password string</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">app.get(<span class="string">&#x27;/register&#x27;</span>,<span class="function">(<span class="params">request,response</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="comment">//定义sql语句</span></span><br><span class="line">    <span class="comment">//王数据库添加账号密码信息</span></span><br><span class="line">    <span class="keyword">var</span> obj = &#123;</span><br><span class="line">        <span class="attr">username</span>:request.query.username,</span><br><span class="line">        <span class="attr">password</span>:request.query.password</span><br><span class="line">    &#125;  </span><br><span class="line">    <span class="keyword">let</span> sql = <span class="string">&quot;SELECT * FROM login_table&quot;</span></span><br><span class="line">    db.query(sql,<span class="function">(<span class="params">err,res</span>)=&gt;</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(err)&#123;</span><br><span class="line">            <span class="keyword">throw</span> err</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&quot;==================================&quot;</span>)</span><br><span class="line">            <span class="comment">// console.log(res)</span></span><br><span class="line">            <span class="comment">//筛选出满足条件的username</span></span><br><span class="line">            <span class="comment">//定义一个标志位flag</span></span><br><span class="line">            <span class="keyword">let</span> flag = <span class="literal">false</span></span><br><span class="line">            <span class="keyword">var</span> bool = res.filter(<span class="function">(<span class="params">item</span>)=&gt;</span>&#123;</span><br><span class="line">                <span class="keyword">if</span>(item.username == obj.username)&#123;</span><br><span class="line">                    flag = <span class="literal">true</span></span><br><span class="line">                    <span class="keyword">return</span> item</span><br><span class="line">                &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                    <span class="comment">//else</span></span><br><span class="line">                    <span class="keyword">return</span> item</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;)</span><br><span class="line">            <span class="built_in">console</span>.log(bool)</span><br><span class="line">            <span class="comment">// console.log(flag)</span></span><br><span class="line">            <span class="keyword">let</span> reg_status = &#123;</span><br><span class="line">                <span class="attr">meta</span>:<span class="string">&quot;该用户名已被使用！&quot;</span>,</span><br><span class="line">                <span class="attr">reg_status</span>:<span class="number">203</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(flag)&#123;</span><br><span class="line">                response.send(reg_status)</span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                response.send(<span class="string">&quot;可以使用用户名&quot;</span>)</span><br><span class="line">                <span class="comment">//并把用户名添加到数据库中</span></span><br><span class="line">                <span class="keyword">let</span> sql_r = <span class="string">`INSERT INTO login_table (username,password)</span></span><br><span class="line"><span class="string">							 VALUES (&#x27;<span class="subst">$&#123;obj.username&#125;</span>&#x27;,&#x27;<span class="subst">$&#123;obj.password&#125;</span>&#x27;)`</span></span><br><span class="line">                <span class="comment">//连接数据库，往里面写账号信息</span></span><br><span class="line">                db.query(sql_r,<span class="function">(<span class="params">err,res</span>)=&gt;</span>&#123;</span><br><span class="line">                    <span class="keyword">if</span>(err)&#123;</span><br><span class="line">                        <span class="keyword">throw</span> err</span><br><span class="line">                    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                        <span class="built_in">console</span>.log(res)</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">//监听端口</span></span><br><span class="line">app.listen(<span class="number">8088</span>,<span class="function">()=&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;服务器在8088端口上启动成功&quot;</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></li>
</ul>
<span id="more"></span>

<h3 id="2-接口实现原理，以及数据库存储情况"><a href="#2-接口实现原理，以及数据库存储情况" class="headerlink" title="2.接口实现原理，以及数据库存储情况"></a>2.接口实现原理，以及数据库存储情况</h3><ul>
<li>接口利用URL匹配，路由的形式访问</li>
<li>数据库存储，使用了明文，在后端直接简单的把前端传过来的数据直接写入到数据库中</li>
<li>数据库可能会被攻击，一旦被攻击，数据库将会直接暴露出来，很危险</li>
</ul>
<h3 id="3-原理图"><a href="#3-原理图" class="headerlink" title="3.原理图"></a>3.原理图</h3><ul>
<li><p>后端对<code>password</code>等敏感信息不作处理，很危险，数据流图如下所示：</p>
</li>
<li><img src="/blogs/2021/11/04/node-js%E5%AE%9E%E7%8E%B0%E5%90%8E%E7%AB%AF%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C%E6%8E%A5%E5%8F%A3/data_stream.png" class title="前后端数据流图"></li>
</ul>
<h2 id="二-改进后，注册接口的实现"><a href="#二-改进后，注册接口的实现" class="headerlink" title="二.改进后，注册接口的实现"></a>二.改进后，注册接口的实现</h2><h3 id="1-实现注册接口-1"><a href="#1-实现注册接口-1" class="headerlink" title="1.实现注册接口"></a>1.实现注册接口</h3><ul>
<li><p>步骤主要是三大步</p>
<ul>
<li>引入后端需要的相关模块，这里测试接口，因此只是引入了<code>express</code>,和<code>mysql</code></li>
<li>连接数据库，使用<code>mysql</code></li>
<li>写接口</li>
</ul>
</li>
<li><p>本次做的改进就是传参的形式的改变，利用时间戳生成了 不重复的8为user_id（<strong>唯一值</strong>）</p>
<p>以及在对数据传入数据库时做了<strong>加密处理</strong>，进一步保证了数据的安全，隐私性</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//导入express</span></span><br><span class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">&#x27;express&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> &#123;application,request,response&#125; = <span class="built_in">require</span>(<span class="string">&#x27;express&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> app = express()</span><br><span class="line"></span><br><span class="line"><span class="comment">//引入Mysql</span></span><br><span class="line"><span class="keyword">const</span> mysql =<span class="built_in">require</span>(<span class="string">&#x27;mysql&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">//连接数据库</span></span><br><span class="line"><span class="comment">//创建连接</span></span><br><span class="line"><span class="keyword">const</span> db = mysql.createConnection(&#123;</span><br><span class="line">    <span class="attr">host</span>:<span class="string">&#x27;127.0.0.1&#x27;</span>,</span><br><span class="line">    <span class="attr">user</span>:<span class="string">&#x27;root&#x27;</span>,</span><br><span class="line">    <span class="attr">password</span>:<span class="string">&#x27;******9&#x27;</span>,</span><br><span class="line">    <span class="attr">port</span>:<span class="number">3306</span>,</span><br><span class="line">    <span class="attr">database</span>:<span class="string">&#x27;mood_diary&#x27;</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">//解决跨域的问题</span></span><br><span class="line"><span class="comment">//解决跨域问题</span></span><br><span class="line">app.all(<span class="string">&#x27;/*&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params">req, res, next</span>) </span>&#123;</span><br><span class="line">    res.header(<span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span>, <span class="string">&#x27;*&#x27;</span>)</span><br><span class="line">    res.header(<span class="string">&#x27;Access-Control-Allow-Headers&#x27;</span>, <span class="string">&#x27;X-Requested-With, mytoken&#x27;</span>)</span><br><span class="line">    res.header(<span class="string">&#x27;Access-Control-Allow-Headers&#x27;</span>, <span class="string">&#x27;X-Requested-With, Authorization&#x27;</span>)</span><br><span class="line">    res.setHeader(<span class="string">&#x27;Content-Type&#x27;</span>, <span class="string">&#x27;application/json;charset=utf-8&#x27;</span>)</span><br><span class="line">    res.header(<span class="string">&#x27;Access-Control-Allow-Headers&#x27;</span>, <span class="string">&#x27;Content-Type,Content-Length, Authorization, Accept,X-Requested-With&#x27;</span>)</span><br><span class="line">    res.header(<span class="string">&#x27;Access-Control-Allow-Methods&#x27;</span>, <span class="string">&#x27;PUT,POST,GET,DELETE,OPTIONS&#x27;</span>)</span><br><span class="line">    res.header(<span class="string">&#x27;X-Powered-By&#x27;</span>, <span class="string">&#x27; 3.2.1&#x27;</span>)</span><br><span class="line">    <span class="keyword">if</span> (req.method == <span class="string">&#x27;OPTIONS&#x27;</span>) res.send(<span class="number">200</span>)</span><br><span class="line">    <span class="comment">/*让options请求快速返回*/</span> <span class="keyword">else</span> next()</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 生成userId</span></span><br><span class="line"><span class="comment"> * userId打算用时间戳来生成 + 0-9的随机数来生成</span></span><br><span class="line"><span class="comment"> * 封装一个方法getUserId()来获取userId</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns <span class="type">&#123;number&#125;</span> <span class="variable">userid</span></span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getUserId</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> date = <span class="keyword">new</span> <span class="built_in">Date</span>()</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取时间戳</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">var</span> dateline = date.getTime().toString()</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *  console.log(&quot;时间戳长度为:&quot;+dateline.length)</span></span><br><span class="line"><span class="comment">     *  截取时间戳的第6-13位</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">var</span> shortDate = dateline.substring(<span class="number">6</span>,<span class="number">13</span>)</span><br><span class="line">   <span class="comment">/*</span></span><br><span class="line"><span class="comment">    console.log(&quot;原始时间戳为：&quot;+dateline)</span></span><br><span class="line"><span class="comment">    console.log(&quot;截取之后的数字为：&quot;+shortDate)</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="comment">//生成随机数</span></span><br><span class="line">    <span class="keyword">var</span> randomNumber = <span class="built_in">Math</span>.floor(<span class="built_in">Math</span>.random()*<span class="number">10</span>)</span><br><span class="line">    <span class="comment">//console.log(&quot;随机数字为:&quot;+randomNumber)</span></span><br><span class="line">    <span class="keyword">var</span> userid = <span class="built_in">parseInt</span>((randomNumber+shortDate))</span><br><span class="line">    <span class="comment">/*console.log(&quot;最终的userId为:&quot;+userId)</span></span><br><span class="line"><span class="comment">    console.log(&quot;userId类型是:&quot;+typeof userId)*/</span></span><br><span class="line">    <span class="keyword">return</span> userid</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 需要生成userId的时候就调用该函数</span></span><br><span class="line"><span class="comment"> * var res = getUserId()</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 测试接口，连接数据库，简单查询</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">app.get(<span class="string">&#x27;/getdata&#x27;</span>,<span class="function">(<span class="params">requset,response</span>)=&gt;</span>&#123;</span><br><span class="line">        <span class="comment">//连接数据库，执行查询</span></span><br><span class="line">        <span class="keyword">const</span> sql = <span class="string">&#x27;SELECT * FROM login_table&#x27;</span></span><br><span class="line">        db.query(sql,<span class="function">(<span class="params">err,result</span>)=&gt;</span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(err)&#123;</span><br><span class="line">                <span class="built_in">console</span>.log(err)</span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                <span class="built_in">console</span>.log(result)</span><br><span class="line">                response.send(result)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 加密算法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">toCode</span>(<span class="params">str</span>) </span>&#123;  <span class="comment">//加密字符串</span></span><br><span class="line">    <span class="comment">//定义密钥，36个字母和数字</span></span><br><span class="line">    <span class="keyword">let</span> key = <span class="string">&quot;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;</span>;</span><br><span class="line">    <span class="keyword">let</span> l = key.length;  <span class="comment">//获取密钥的长度</span></span><br><span class="line">    <span class="keyword">let</span> a = key.split(<span class="string">&quot;&quot;</span>);  <span class="comment">//把密钥字符串转换为字符数组</span></span><br><span class="line">    <span class="keyword">let</span> s = <span class="string">&quot;&quot;</span>,b, b1, b2, b3;  <span class="comment">//定义临时变量</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt;str.length; i ++) &#123;  <span class="comment">//遍历字符串</span></span><br><span class="line">        b = str.charCodeAt(i);  <span class="comment">//逐个提取每个字符，并获取Unicode编码值</span></span><br><span class="line">        b1 = b % l;         <span class="comment">//求Unicode编码值得余数</span></span><br><span class="line">        b = (b - b1)/l;     <span class="comment">//求最大倍数</span></span><br><span class="line">        b2 = b % l;         <span class="comment">//求最大倍数的于是</span></span><br><span class="line">        b = (b - b2)/l;     <span class="comment">//求最大倍数</span></span><br><span class="line">        b3 = b % l;         <span class="comment">//求最大倍数的余数</span></span><br><span class="line">        s += a[b3] + a[b2] + a[b1];  <span class="comment">//根据余数值映射到密钥中对应下标位置的字符</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> s;  <span class="comment">//返回这些映射的字符</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 解密算法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fromCode</span>(<span class="params">str</span>) </span>&#123;</span><br><span class="line">    <span class="comment">//定义密钥，36个字母和数字</span></span><br><span class="line">    <span class="keyword">let</span> key = <span class="string">&quot;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;</span>;</span><br><span class="line">    <span class="keyword">let</span> l = key.length;  <span class="comment">//获取密钥的长度</span></span><br><span class="line">    <span class="keyword">let</span> b, b1, b2, b3, d = <span class="number">0</span>, s;  <span class="comment">//定义临时变量</span></span><br><span class="line">    s = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="built_in">Math</span>.floor(str.length / <span class="number">3</span>));  <span class="comment">//计算加密字符串包含的字符数，并定义数组</span></span><br><span class="line">    b = s.length;  <span class="comment">//获取数组的长度</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; b; i ++) &#123;  <span class="comment">//以数组的长度循环次数，遍历加密字符串</span></span><br><span class="line">        b1 = key.indexOf(str.charAt(d));  <span class="comment">//截取周期内第一个字符串，计算在密钥中的下标值</span></span><br><span class="line">        d ++;</span><br><span class="line">        b2 = key.indexOf(str.charAt(d));  <span class="comment">//截取周期内第二个字符串，计算在密钥中的下标值</span></span><br><span class="line">        d ++;</span><br><span class="line">        b3 = key.indexOf(str.charAt(d));  <span class="comment">//截取周期内第三个字符串，计算在密钥中的下标值</span></span><br><span class="line">        d ++;</span><br><span class="line">        s[i] = b1 * l * <span class="number">1</span> + b2 * l + b3  <span class="comment">//利用下标值，反推被加密字符的Unicode编码值</span></span><br><span class="line">    &#125;</span><br><span class="line">    b = <span class="built_in">eval</span>(<span class="string">&quot;String.fromCharCode(&quot;</span> + s.join(<span class="string">&#x27;,&#x27;</span>) + <span class="string">&quot;)&quot;</span>);<span class="comment">//用fromCharCode()算出字符串</span></span><br><span class="line">    <span class="keyword">return</span> b ;  <span class="comment">//返回被解密的字符串</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 注册账号接口</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">app.get(<span class="string">&#x27;/register/&#x27;</span>,<span class="function">(<span class="params">request,response</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="keyword">const</span>  userId = getUserId()         <span class="comment">//生成userId</span></span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;生成的userId为：&quot;</span>+userId)</span><br><span class="line">    <span class="comment">//先创建对象接收参数</span></span><br><span class="line">    <span class="keyword">let</span> regParams = &#123;</span><br><span class="line">        <span class="attr">userId</span>:userId,</span><br><span class="line">        <span class="attr">username</span>:request.query.username,</span><br><span class="line">        <span class="comment">//调用加密函数进行加密</span></span><br><span class="line">        <span class="attr">password</span>:toCode(request.query.password)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//定义一个成功状态标志对象</span></span><br><span class="line">    <span class="keyword">var</span> status_s = &#123;</span><br><span class="line">        <span class="attr">meta</span>:<span class="string">&#x27;昵称可用&#x27;</span>,</span><br><span class="line">        <span class="attr">status</span>:<span class="number">200</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//定义一个失败状态标志对象</span></span><br><span class="line">    <span class="keyword">var</span> status_f = &#123;</span><br><span class="line">        <span class="attr">meta</span>:<span class="string">&#x27;昵称重复，不可用,请换一个昵称试试！&#x27;</span>,</span><br><span class="line">        <span class="attr">status</span>:<span class="number">201</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">console</span>.log(userId,regParams.username,regParams.password)</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 在执行插入语句之前，先执行查询语句，如果数据库中有相同的用户名，则注册失败</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">const</span> mysql_select = <span class="string">`SELECT * FROM login_table WHERE username = &#x27;<span class="subst">$&#123;regParams.username&#125;</span>&#x27;`</span></span><br><span class="line">    db.query(mysql_select,<span class="function">(<span class="params">err,result</span>)=&gt;</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(err)&#123;</span><br><span class="line">            <span class="keyword">throw</span> err</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">/**</span></span><br><span class="line"><span class="comment">             * 如果查询到的是一个空对象，那么证明没有改记录，可以继续注册</span></span><br><span class="line"><span class="comment">             * 如果不是空对象，则查询成功，昵称重复，不可以注册，得换一个昵称</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">            <span class="keyword">let</span> arr = <span class="built_in">Object</span>.keys(result)</span><br><span class="line">            <span class="keyword">if</span>(arr.length == <span class="number">0</span>)&#123;</span><br><span class="line">                <span class="built_in">console</span>.log(<span class="string">&quot;昵称可用！&quot;</span>)</span><br><span class="line">                <span class="comment">/**</span></span><br><span class="line"><span class="comment">                 * sql语句插入向表中插入账户和密码</span></span><br><span class="line"><span class="comment">                 */</span></span><br><span class="line">                <span class="keyword">const</span> mysql = <span class="string">`INSERT INTO login_table (user_id,username,password) </span></span><br><span class="line"><span class="string">                   VALUES (&#x27;<span class="subst">$&#123;regParams.userId&#125;</span>&#x27;,&#x27;<span class="subst">$&#123;regParams.username&#125;</span>&#x27;,&#x27;<span class="subst">$&#123;regParams.password&#125;</span>&#x27;)`</span></span><br><span class="line">                <span class="comment">//执行插入操作</span></span><br><span class="line">                db.query(mysql,<span class="function">(<span class="params">err,res</span>)=&gt;</span>&#123;</span><br><span class="line">                    <span class="keyword">if</span>(err)&#123;</span><br><span class="line">                        <span class="keyword">throw</span> err</span><br><span class="line">                    &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                        <span class="built_in">console</span>.log(res)</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;)</span><br><span class="line">                response.send(status_s)</span><br><span class="line">            &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="built_in">console</span>.log(<span class="string">&quot;昵称重复，不可用&quot;</span>)</span><br><span class="line">                response.send(status_f)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></li>
<li><p>本次注册接口，新增了两个算法和一个利用时间戳和随机数生成userId的函数</p>
<ul>
<li>我封装了一个<strong>加密算法</strong></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 加密算法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">toCode</span>(<span class="params">str</span>) </span>&#123;  <span class="comment">//加密字符串</span></span><br><span class="line">    <span class="comment">//定义密钥，36个字母和数字</span></span><br><span class="line">    <span class="keyword">let</span> key = <span class="string">&quot;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;</span>;</span><br><span class="line">    <span class="keyword">let</span> l = key.length;  <span class="comment">//获取密钥的长度</span></span><br><span class="line">    <span class="keyword">let</span> a = key.split(<span class="string">&quot;&quot;</span>);  <span class="comment">//把密钥字符串转换为字符数组</span></span><br><span class="line">    <span class="keyword">let</span> s = <span class="string">&quot;&quot;</span>,b, b1, b2, b3;  <span class="comment">//定义临时变量</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt;str.length; i ++) &#123;  <span class="comment">//遍历字符串</span></span><br><span class="line">        b = str.charCodeAt(i);  <span class="comment">//逐个提取每个字符，并获取Unicode编码值</span></span><br><span class="line">        b1 = b % l;         <span class="comment">//求Unicode编码值得余数</span></span><br><span class="line">        b = (b - b1)/l;     <span class="comment">//求最大倍数</span></span><br><span class="line">        b2 = b % l;         <span class="comment">//求最大倍数的于是</span></span><br><span class="line">        b = (b - b2)/l;     <span class="comment">//求最大倍数</span></span><br><span class="line">        b3 = b % l;         <span class="comment">//求最大倍数的余数</span></span><br><span class="line">        s += a[b3] + a[b2] + a[b1];  <span class="comment">//根据余数值映射到密钥中对应下标位置的字符</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> s;  <span class="comment">//返回这些映射的字符</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>封装了一个<strong>解密算法</strong></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 解密算法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fromCode</span>(<span class="params">str</span>) </span>&#123;</span><br><span class="line">    <span class="comment">//定义密钥，36个字母和数字</span></span><br><span class="line">    <span class="keyword">let</span> key = <span class="string">&quot;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;</span>;</span><br><span class="line">    <span class="keyword">let</span> l = key.length;  <span class="comment">//获取密钥的长度</span></span><br><span class="line">    <span class="keyword">let</span> b, b1, b2, b3, d = <span class="number">0</span>, s;  <span class="comment">//定义临时变量</span></span><br><span class="line">    s = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="built_in">Math</span>.floor(str.length / <span class="number">3</span>));  <span class="comment">//计算加密字符串包含的字符数，并定义数组</span></span><br><span class="line">    b = s.length;  <span class="comment">//获取数组的长度</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; b; i ++) &#123;  <span class="comment">//以数组的长度循环次数，遍历加密字符串</span></span><br><span class="line">        b1 = key.indexOf(str.charAt(d));  <span class="comment">//截取周期内第一个字符串，计算在密钥中的下标值</span></span><br><span class="line">        d ++;</span><br><span class="line">        b2 = key.indexOf(str.charAt(d));  <span class="comment">//截取周期内第二个字符串，计算在密钥中的下标值</span></span><br><span class="line">        d ++;</span><br><span class="line">        b3 = key.indexOf(str.charAt(d));  <span class="comment">//截取周期内第三个字符串，计算在密钥中的下标值</span></span><br><span class="line">        d ++;</span><br><span class="line">        s[i] = b1 * l * <span class="number">1</span> + b2 * l + b3  <span class="comment">//利用下标值，反推被加密字符的Unicode编码值</span></span><br><span class="line">    &#125;</span><br><span class="line">    b = <span class="built_in">eval</span>(<span class="string">&quot;String.fromCharCode(&quot;</span> + s.join(<span class="string">&#x27;,&#x27;</span>) + <span class="string">&quot;)&quot;</span>);<span class="comment">//用fromCharCode()算出字符串</span></span><br><span class="line">    <span class="keyword">return</span> b ;  <span class="comment">//返回被解密的字符串</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>生成<strong>唯一的userId</strong></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 生成userId</span></span><br><span class="line"><span class="comment"> * userId打算用时间戳来生成 + 0-9的随机数来生成</span></span><br><span class="line"><span class="comment"> * 封装一个方法getUserId()来获取userId</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@returns <span class="type">&#123;number&#125;</span> <span class="variable">userid</span></span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getUserId</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> date = <span class="keyword">new</span> <span class="built_in">Date</span>()</span><br><span class="line">    <span class="comment">/** 获取时间戳</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">var</span> dateline = date.getTime().toString()</span><br><span class="line">    <span class="comment">/**  截取时间戳的第6-13位</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">var</span> shortDate = dateline.substring(<span class="number">6</span>,<span class="number">13</span>)</span><br><span class="line">    <span class="comment">//生成随机数</span></span><br><span class="line">    <span class="keyword">var</span> randomNumber = <span class="built_in">Math</span>.floor(<span class="built_in">Math</span>.random()*<span class="number">10</span>)</span><br><span class="line">    <span class="comment">//console.log(&quot;随机数字为:&quot;+randomNumber)</span></span><br><span class="line">    <span class="keyword">var</span> userid = <span class="built_in">parseInt</span>((randomNumber+shortDate))</span><br><span class="line">    <span class="comment">/*console.log(&quot;最终的userId为:&quot;+userId)</span></span><br><span class="line"><span class="comment">    console.log(&quot;userId类型是:&quot;+typeof userId)*/</span></span><br><span class="line">    <span class="keyword">return</span> userid</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="2-接口实现原理"><a href="#2-接口实现原理" class="headerlink" title="2.接口实现原理"></a>2.接口实现原理</h3><ul>
<li>接口利用URL匹配，路由的形式访问</li>
<li>数据库存储，使用了密文，在后端把前端传过来的数据加密后再写入到数据库中</li>
<li>数据库可能会被攻击，最多暴露了密文，没有密钥和解密算法，不会暴露password和username信息</li>
</ul>
<h3 id="3-原理图-1"><a href="#3-原理图-1" class="headerlink" title="3.原理图"></a>3.原理图</h3><ul>
<li><p>后端对<code>password</code>等敏感信息作了处理，增加了安全性，数据流图如下所示：</p>
</li>
<li><img src="/blogs/2021/11/04/node-js%E5%AE%9E%E7%8E%B0%E5%90%8E%E7%AB%AF%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C%E6%8E%A5%E5%8F%A3/data_stream_s.png" class title="后端加密后的前后端数据流图"></li>
</ul>
<h2 id="三-前端测试"><a href="#三-前端测试" class="headerlink" title="三.前端测试"></a>三.前端测试</h2><ul>
<li><p>前端采用vue来写，数据请求使用axioxs封装了Ajax,前端js测试部分代码如下</p>
</li>
<li><p><strong><code>axios.defaults.baseURL = &#39;http://127.0.0.1:8086/&#39;</code></strong></p>
<p><strong><code>Vue.prototype.$http = axios</code></strong></p>
</li>
<li><p>axios已经挂载vue的原型上了</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//注册账户</span></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="title">registerCount</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">this</span>.form.username==<span class="string">&#x27;&#x27;</span>||<span class="built_in">this</span>.form.password==<span class="string">&#x27;&#x27;</span>)&#123;</span><br><span class="line">        <span class="built_in">this</span>.$message.error(<span class="string">&quot;请先输入账号和密码再进行注册&quot;</span>)</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="keyword">const</span> res = <span class="keyword">await</span> <span class="built_in">this</span>.$http.get(<span class="string">&#x27;/register&#x27;</span>,&#123;<span class="attr">params</span>:<span class="built_in">this</span>.form&#125;)</span><br><span class="line">        <span class="built_in">console</span>.log(res.data)</span><br><span class="line">        <span class="keyword">if</span>(res.data.status != <span class="number">200</span>)&#123;</span><br><span class="line">            <span class="built_in">this</span>.$message.error(res.data.meta)</span><br><span class="line">            <span class="built_in">this</span>.form.username = <span class="string">&#x27;&#x27;</span></span><br><span class="line">            <span class="built_in">this</span>.form.password = <span class="string">&#x27;&#x27;</span></span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="comment">//注册成功，提示注册成功</span></span><br><span class="line">            <span class="built_in">this</span>.$message.success(<span class="string">&quot;注册成功，请牢记密码&quot;</span>)</span><br><span class="line">            <span class="comment">//置空输入框</span></span><br><span class="line">            <span class="built_in">this</span>.form.username = <span class="string">&#x27;&#x27;</span></span><br><span class="line">            <span class="built_in">this</span>.form.password = <span class="string">&#x27;&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure></li>
<li><p>后端显示</p>
<p>如下图</p>
<img src="/blogs/2021/11/04/node-js%E5%AE%9E%E7%8E%B0%E5%90%8E%E7%AB%AF%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C%E6%8E%A5%E5%8F%A3/back.png" class title="后端数据查询"></li>
<li><p>数据库显示</p>
<p>如下图</p>
<img src="/blogs/2021/11/04/node-js%E5%AE%9E%E7%8E%B0%E5%90%8E%E7%AB%AF%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C%E6%8E%A5%E5%8F%A3/database.png" class title="数据库显示密文存储"></li>
</ul>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="http://chaoyxy.cn/data/User/admin/home/desktop/images/alipay.jpg" alt=" 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="http://chaoyxy.cn/data/User/admin/home/desktop/images/wechatpay.jpg" alt=" 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blogs/tags/Node-js/" rel="tag"># Node.js</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blogs/2021/10/30/node-js%E5%90%8E%E7%AB%AF%E6%8E%A5%E6%94%B6%E5%AF%B9%E8%B1%A1%E5%8F%82%E6%95%B0%E7%9A%84%E5%A4%84%E7%90%86/" rel="next" title="node.js后端接收对象参数的处理">
                <i class="fa fa-chevron-left"></i> node.js后端接收对象参数的处理
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blogs/2021/11/05/vue%E9%A1%B5%E9%9D%A2%E4%BC%A0%E5%80%BC%E5%92%8C%E4%BC%9A%E8%AF%9D%E5%AD%98%E5%82%A8/" rel="prev" title="vue页面传值和会话存储">
                vue页面传值和会话存储 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a target="_blank" rel="noopener" href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blogs/images/cat.jpg"
                alt="" />
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blogs/archives">
              
                  <span class="site-state-item-count">43</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blogs/tags/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/louchaoyy" target="_blank" title="Gitee">
                      
                        <i class="fa fa-fw fa-gitee"></i>Gitee</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="louchao@louchaoyy.cn" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Node-js%E5%AE%9E%E7%8E%B0%E7%9A%84%E5%90%8E%E7%AB%AF%E6%B3%A8%E5%86%8C%E5%92%8C%E7%99%BB%E5%BD%95%E6%8E%A5%E5%8F%A3%EF%BC%88%E6%9B%B4%E6%96%B0%E5%90%8E-%E9%99%84%E5%89%8D%E7%AB%AF%E6%B5%8B%E8%AF%95%E4%BB%A3%E7%A0%81%EF%BC%89"><span class="nav-number">1.</span> <span class="nav-text">Node.js实现的后端注册和登录接口（更新后-附前端测试代码）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%80-%E4%BB%A5%E5%89%8D%E6%8E%A5%E5%8F%A3%E5%AE%9E%E7%8E%B0"><span class="nav-number">2.</span> <span class="nav-text">一.以前接口实现</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E5%AE%9E%E7%8E%B0%E6%B3%A8%E5%86%8C%E6%8E%A5%E5%8F%A3"><span class="nav-number">2.1.</span> <span class="nav-text">1.实现注册接口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E6%8E%A5%E5%8F%A3%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86%EF%BC%8C%E4%BB%A5%E5%8F%8A%E6%95%B0%E6%8D%AE%E5%BA%93%E5%AD%98%E5%82%A8%E6%83%85%E5%86%B5"><span class="nav-number">2.2.</span> <span class="nav-text">2.接口实现原理，以及数据库存储情况</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E5%8E%9F%E7%90%86%E5%9B%BE"><span class="nav-number">2.3.</span> <span class="nav-text">3.原理图</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8C-%E6%94%B9%E8%BF%9B%E5%90%8E%EF%BC%8C%E6%B3%A8%E5%86%8C%E6%8E%A5%E5%8F%A3%E7%9A%84%E5%AE%9E%E7%8E%B0"><span class="nav-number">3.</span> <span class="nav-text">二.改进后，注册接口的实现</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E5%AE%9E%E7%8E%B0%E6%B3%A8%E5%86%8C%E6%8E%A5%E5%8F%A3-1"><span class="nav-number">3.1.</span> <span class="nav-text">1.实现注册接口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E6%8E%A5%E5%8F%A3%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86"><span class="nav-number">3.2.</span> <span class="nav-text">2.接口实现原理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E5%8E%9F%E7%90%86%E5%9B%BE-1"><span class="nav-number">3.3.</span> <span class="nav-text">3.原理图</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%89-%E5%89%8D%E7%AB%AF%E6%B5%8B%E8%AF%95"><span class="nav-number">4.</span> <span class="nav-text">三.前端测试</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Chaoyxy</span>

  
</div>

<span id="busuanzi_container_site_pv">
    本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<!--

  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.3</div>
-->




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/blogs/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/blogs/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/blogs/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/blogs/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/blogs/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/blogs/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/blogs/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/blogs/js/src/bootstrap.js?v=5.1.3"></script>



  


  

    
      <script id="dsq-count-scr" src="https://Chaoyxy.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'https://louchaoyy.gitee.io/blogs/2021/11/04/node-js%E5%AE%9E%E7%8E%B0%E5%90%8E%E7%AB%AF%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C%E6%8E%A5%E5%8F%A3/';
          this.page.identifier = '2021/11/04/node-js实现后端登录注册接口/';
          this.page.title = 'node.js实现后端登录注册接口';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://Chaoyxy.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  





  

  

  

  
  

  

  

  

</body>
</html>
